<template>
  <div class="monitoring-platform">
    <!-- 全局导航 -->
    <Navigation />

    <div class="main-layout">
      <!-- 左侧边栏 -->
      <aside class="sidebar">
        <div class="sidebar-header">
          <h2>数据监控</h2>
        </div>
        
        <nav class="sidebar-nav">
          <router-link to="/monitoring/realtime" class="sidebar-item" :class="{ active: activeTab === 'realtime' }">
            <i class="icon-realtime"></i>
            <span>实时数据</span>
          </router-link>
          <router-link to="/monitoring/history" class="sidebar-item" :class="{ active: activeTab === 'history' }">
            <i class="icon-history"></i>
            <span>历史数据</span>
          </router-link>
          <router-link to="/monitoring/trend" class="sidebar-item" :class="{ active: activeTab === 'trend' }">
            <i class="icon-trend"></i>
            <span>趋势分析</span>
          </router-link>
          <router-link to="/monitoring/heatmap" class="sidebar-item" :class="{ active: activeTab === 'heatmap' }">
            <i class="icon-heatmap"></i>
            <span>压力热图</span>
          </router-link>
        </nav>
      </aside>

      <!-- 主内容区域 -->
      <main class="main-content">
        <!-- 数据统计卡片 -->
        <section class="stats-section">
          <div class="stats-grid">
            <StatCard
              label="在线瓷砖数"
              :value="186"
              unit="块"
              icon-class="icon-online"
              :chart-data="[15, 5, 10, 8]"
            />
            
            <StatCard
              label="异常信号"
              :value="3"
              unit="处"
              icon-class="icon-alert"
              :chart-data="[20, 15, 18, 12]"
            />
            
            <StatCard
              label="平均压力值"
              :value="2.8"
              unit="kPa"
              icon-class="icon-pressure"
              :chart-data="[18, 12, 15, 10]"
            />
            
            <StatCard
              label="今日信号总量"
              :value="24568"
              unit="个"
              icon-class="icon-signal"
              :chart-data="[22, 8, 12, 6]"
            />
          </div>
        </section>

        <!-- 热图和图表区域 -->
        <section class="charts-section">
          <div class="charts-grid">
            <!-- 瓷砖压力分布热图 -->
            <HeatmapChart
              :heatmap-data="heatmapData"
              @area-filter="handleAreaFilter"
              @filter="handleFilter"
              @reset-view="handleResetView"
              @cell-click="handleCellClick"
              @toggle-2d="handleToggle2D"
              @toggle-zoom="handleToggleZoom"
            />

            <!-- 实时压力曲线 -->
            <div class="chart-card pressure-chart">
              <div class="chart-header">
                <h3>实时压力曲线</h3>
              </div>
              
              <div class="pressure-chart-container">
                <svg class="pressure-svg" viewBox="0 0 225 320">
                  <!-- 网格线 -->
                  <defs>
                    <pattern id="grid" width="25" height="32" patternUnits="userSpaceOnUse">
                      <path d="M 25 0 L 0 0 0 32" fill="none" stroke="#f0f0f0" stroke-width="1"/>
                    </pattern>
                  </defs>
                  <rect width="100%" height="100%" fill="url(#grid)" />
                  
                  <!-- 压力曲线 -->
                  <path d="M10,280 Q60,200 120,220 T210,180" stroke="#1677FF" stroke-width="3" fill="none"/>
                  <path d="M10,250 Q60,180 120,200 T210,160" stroke="#FF4D4F" stroke-width="2" fill="none"/>
                  <path d="M10,290 Q60,240 120,260 T210,200" stroke="#52C41A" stroke-width="2" fill="none"/>
                  
                  <!-- Y轴标签 -->
                  <text x="5" y="50" font-size="10" fill="#666">7.0</text>
                  <text x="5" y="100" font-size="10" fill="#666">5.5</text>
                  <text x="5" y="150" font-size="10" fill="#666">4.0</text>
                  <text x="5" y="200" font-size="10" fill="#666">2.5</text>
                  <text x="5" y="250" font-size="10" fill="#666">1.0</text>
                  
                  <!-- X轴标签 -->
                  <text x="30" y="315" font-size="10" fill="#666">00:00</text>
                  <text x="90" y="315" font-size="10" fill="#666">06:00</text>
                  <text x="150" y="315" font-size="10" fill="#666">12:00</text>
                  <text x="200" y="315" font-size="10" fill="#666">18:00</text>
                </svg>
              </div>
            </div>
          </div>
        </section>

        <!-- 控制面板 -->
        <section class="control-panel">
          <div class="time-controls">
            <span class="control-label">时间范围：</span>
            <button class="time-btn active">实时</button>
            <button class="time-btn dropdown">1小时</button>
            
            <div class="divider"></div>
            
            <select class="control-select">
              <option>全部压力范围</option>
            </select>
            
            <select class="control-select">
              <option>全部区域</option>
            </select>
          </div>
          
          <div class="action-controls">
            <button class="action-btn">
              <i class="icon-refresh"></i>
              <span>刷新</span>
            </button>
            
            <div class="auto-refresh">
              <div class="toggle-switch active"></div>
              <span>自动 (13s)</span>
            </div>
            
            <button class="action-btn primary">
              <i class="icon-export"></i>
              <span>导出数据</span>
            </button>
          </div>
        </section>

        <!-- 异常数据列表 -->
        <DataTable
          title="异常数据列表"
          :columns="tableColumns"
          :data="tableData"
          @view-detail="handleViewDetail"
          @mark-processed="handleMarkProcessed"
        />
      </main>
    </div>
  </div>
</template>

<script>
import StatCard from './components/StatCard.vue'
import HeatmapChart from './components/HeatmapChart.vue'
import DataTable from './components/DataTable.vue'
import Navigation from '../components/Navigation.vue'

export default {
  name: 'MiaoduoPage',
  components: {
    StatCard,
    HeatmapChart,
    DataTable,
    Navigation
  },
  data() {
    return {
      // 热图数据
      heatmapData: this.generateHeatmapData(),
      
      // 表格列定义
      tableColumns: [
        { key: 'tileId', title: '瓷砖ID' },
        { key: 'position', title: '位置' },
        { key: 'detectionTime', title: '检测时间' },
        { key: 'pressure', title: '压力值 (kPa)' },
        { key: 'status', title: '状态' },
        { key: 'actions', title: '操作' }
      ],
      
      // 表格数据
      tableData: [
        {
          id: 1,
          tileId: 'T-10245',
          position: '区域A-2-3',
          detectionTime: '2023-11-28 14:23:45',
          pressure: '6.72',
          status: '严重异常'
        },
        {
          id: 2,
          tileId: 'T-10126',
          position: '区域B-1-5',
          detectionTime: '2023-11-28 14:15:12',
          pressure: '5.89',
          status: '轻微异常'
        },
        {
          id: 3,
          tileId: 'T-10078',
          position: '区域A-3-7',
          detectionTime: '2023-11-28 14:02:37',
          pressure: '5.24',
          status: '轻微异常'
        },
        {
          id: 4,
          tileId: 'T-10315',
          position: '区域C-2-2',
          detectionTime: '2023-11-28 13:58:19',
          pressure: '7.16',
          status: '严重异常'
        },
        {
          id: 5,
          tileId: 'T-09987',
          position: '区域B-4-1',
          detectionTime: '2023-11-28 13:47:05',
          pressure: '4.95',
          status: '正常'
        }
      ]
    }
  },
  computed: {
    activeTab() {
      // 根据路由元信息或路径确定当前活动的标签页
      if (this.$route.meta && this.$route.meta.activeTab) {
        return this.$route.meta.activeTab
      }
      
      // 根据路径判断
      const path = this.$route.path
      if (path.includes('/realtime')) return 'realtime'
      if (path.includes('/history')) return 'history'
      if (path.includes('/trend')) return 'trend'
      if (path.includes('/heatmap')) return 'heatmap'
      
      // 默认为监控
      return 'monitoring'
    }
  },
  methods: {
    generateHeatmapData() {
      const data = [];
      for (let row = 0; row < 10; row++) {
        const rowData = [];
        for (let col = 0; col < 10; col++) {
          const pressure = Math.random() * 7;
          rowData.push({
            id: `T-${10000 + row * 10 + col}`,
            position: `${String.fromCharCode(65 + Math.floor(row / 3))}-${row % 3}-${col}`,
            pressure: pressure.toFixed(1)
          });
        }
        data.push(rowData);
      }
      return data;
    },
    
    getPressureLevel(pressure) {
      const value = parseFloat(pressure);
      if (value < 2) return 'pressure-low';
      if (value < 4) return 'pressure-medium';
      if (value < 6) return 'pressure-high';
      return 'pressure-critical';
    },
    
    // 热图事件处理
    handleAreaFilter() {
      console.log('区域筛选');
    },
    
    handleFilter() {
      console.log('筛选');
    },
    
    handleResetView() {
      console.log('重置视图');
    },
    
    handleCellClick(cell) {
      console.log('点击瓷砖:', cell);
    },
    
    handleToggle2D() {
      console.log('切换2D视图');
    },
    
    handleToggleZoom() {
      console.log('切换缩放视图');
    },
    
    // 表格事件处理
    handleViewDetail(item) {
      console.log('查看详情:', item);
    },
    
    handleMarkProcessed(item) {
      console.log('标记处理:', item);
    }
  }
}
</script>

<style scoped>
.monitoring-platform {
  min-height: 100vh;
  background: #F5F5F5;
  font-family: 'Inter', sans-serif;
}


/* 主布局 */
.main-layout {
  display: flex;
  min-height: calc(100vh - 66px);
}

/* 左侧边栏 */
.sidebar {
  width: 220px;
  background: #FFFFFF;
  border-right: 1px solid #E5E7EB;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid #E5E7EB;
}

.sidebar-header h2 {
  font-size: 16px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.88);
  margin: 0;
}

.sidebar-nav {
  padding: 4px;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 24px;
  color: rgba(0, 0, 0, 0.88);
  text-decoration: none;
  font-size: 14px;
  border-radius: 8px;
  margin: 0 4px;
}

.sidebar-item.active {
  background: #E6F4FF;
  color: #1677FF;
}

.sidebar-item i::before {
  font-size: 14px;
}

.icon-realtime::before { content: "📊"; }
.icon-history::before { content: "📈"; }
.icon-trend::before { content: "📉"; }
.icon-heatmap::before { content: "🗺️"; }

/* 主内容区域 */
.main-content {
  flex: 1;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 数据统计卡片 */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
}

/* 图表区域 */
.charts-grid {
  display: grid;
  grid-template-columns: 1fr 275px;
  gap: 24px;
}

.chart-card {
  background: #FFFFFF;
  border: 1px solid #F0F0F0;
  border-radius: 8px;
  overflow: hidden;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 24px;
  border-bottom: 1px solid #F0F0F0;
}

.chart-header h3 {
  font-size: 16px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.88);
  margin: 0;
}

.chart-controls {
  display: flex;
  gap: 8px;
}

.control-btn {
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  border-radius: 6px;
  padding: 5px 16px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
  cursor: pointer;
  box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.02);
}


/* 压力图表 */
.pressure-chart-container {
  padding: 24px;
}

.pressure-svg {
  width: 100%;
  height: 320px;
}

/* 控制面板 */
.control-panel {
  background: #FFFFFF;
  border: 1px solid #F0F0F0;
  border-radius: 8px;
  padding: 25px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.time-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.control-label {
  color: rgba(0, 0, 0, 0.88);
  font-size: 14px;
}

.time-btn {
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  border-radius: 6px;
  padding: 5px 16px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
  cursor: pointer;
}

.time-btn.active {
  background: #1677FF;
  color: #FFFFFF;
  border-color: transparent;
  box-shadow: 0px 2px 0px 0px rgba(5, 145, 255, 0.10);
}

.divider {
  width: 1px;
  height: 24px;
  background: #F0F0F0;
}

.control-select {
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
  cursor: pointer;
}

.action-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.action-btn {
  background: #FFFFFF;
  border: 1px solid #D9D9D9;
  border-radius: 6px;
  padding: 5px 16px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.02);
}

.action-btn.primary {
  background: #1677FF;
  color: #FFFFFF;
  border-color: transparent;
  box-shadow: 0px 2px 0px 0px rgba(5, 145, 255, 0.10);
}

.auto-refresh {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
}

.toggle-switch {
  width: 30px;
  height: 20px;
  background: #1677FF;
  border-radius: 10px;
  position: relative;
  cursor: pointer;
}

.toggle-switch::after {
  content: '';
  position: absolute;
  top: 2px;
  right: 2px;
  width: 16px;
  height: 16px;
  background: #FFFFFF;
  border-radius: 50%;
}

.icon-refresh::before { content: "🔄"; }
.icon-export::before { content: "📤"; }


/* 响应式设计 */
@media (max-width: 1400px) {
  .header-content {
    padding: 0 16px;
  }
  
  .main-content {
    padding: 16px;
  }
}

@media (max-width: 1200px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .charts-grid {
    grid-template-columns: 1fr;
  }
  
  .platform-title {
    width: auto;
    min-width: 120px;
  }
  
  .nav-menu {
    margin-left: 100px;
  }
}

@media (max-width: 992px) {
  .header-content {
    flex-wrap: wrap;
    height: auto;
    padding: 12px 16px;
    gap: 12px;
  }
  
  .platform-title {
    order: 1;
    width: 100%;
    margin-bottom: 8px;
  }
  
  .nav-menu {
    order: 2;
    margin-left: 0;
    width: 100%;
    justify-content: flex-start;
    gap: 24px;
  }
  
  .header-actions {
    order: 3;
    margin-left: 0;
    width: 100%;
    justify-content: space-between;
  }
  
  .search-box {
    flex: 1;
    max-width: 300px;
  }
  
  .main-layout {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100%;
    order: 2;
  }
  
  .main-content {
    order: 1;
  }
  
  .control-panel {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .time-controls {
    flex-wrap: wrap;
    gap: 8px;
  }
  
  .action-controls {
    justify-content: center;
    flex-wrap: wrap;
  }
}

@media (max-width: 768px) {
  .stats-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .charts-section {
    gap: 16px;
  }
  
  .header-content {
    padding: 8px 12px;
  }
  
  .main-content {
    padding: 12px;
    gap: 16px;
  }
  
  .sidebar {
    position: fixed;
    top: 0;
    left: -220px;
    height: 100vh;
    z-index: 1000;
    transition: left 0.3s ease;
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  }
  
  .sidebar.open {
    left: 0;
  }
  
  .nav-menu {
    flex-direction: column;
    gap: 8px;
    align-items: flex-start;
  }
  
  .nav-item {
    padding: 8px 0;
  }
  
  .header-actions {
    gap: 8px;
  }
  
  .search-box {
    width: 150px;
  }
}

@media (max-width: 480px) {
  .header-content {
    padding: 8px;
  }
  
  .main-content {
    padding: 8px;
    gap: 12px;
  }
  
  .platform-title {
    font-size: 16px;
  }
  
  .nav-menu {
    gap: 16px;
  }
  
  .header-actions {
    flex-wrap: wrap;
    gap: 6px;
  }
  
  .search-box {
    width: 120px;
    padding: 4px 8px;
  }
  
  .search-box input {
    font-size: 12px;
  }
  
  .notification-btn, 
  .message-btn, 
  .user-avatar {
    width: 28px;
    height: 28px;
  }
  
  .control-panel {
    padding: 16px;
  }
  
  .time-controls,
  .action-controls {
    gap: 6px;
  }
  
  .control-btn,
  .time-btn,
  .action-btn {
    padding: 4px 12px;
    font-size: 12px;
  }
}

/* 打印样式 */
@media print {
  .header-actions,
  .sidebar,
  .control-panel {
    display: none;
  }
  
  .main-layout {
    flex-direction: column;
  }
  
  .main-content {
    padding: 0;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .charts-grid {
    grid-template-columns: 1fr;
  }
}
</style>
